<template>
  <el-dialog
    :title="titleMap[mode]"
    v-model="visible"
    width="50%"
    destroy-on-close
    @closed="$emit('closed')"
  >
    <el-form
      :model="form"
      :rules="rules"
      ref="dialogForm"
      :disabled="mode == 'show'"
      label-width="auto"
      label-position="right"
    >
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="form.title"
          clearable
          placeholder="请输入标题"
        ></el-input>
      </el-form-item>
      <el-form-item label="内容" prop="text" style="width: 100%">
        <el-input
          v-model="form.text"
          clearable
          placeholder="请输入内容"
          type="textarea"
          rows="5"
        ></el-input>
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker
          v-model="form.startTime"
          type="date"
          placeholder="请选择开始日期"
          value-format="YYYY-MM-DD"
          :disabled-date="disabledDate"
          style="width: 100%"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker
          v-model="form.endTime"
          type="date"
          placeholder="请选择结束时间"
          value-format="YYYY-MM-DD"
          :disabled-date="disabledDate"
          style="width: 100%"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="是否启用" prop="status" style="width: 100%">
        <el-switch
          v-model="form.status"
          :inactive-value="1"
          :active-value="0"
        />
      </el-form-item>
    </el-form>

    <template #footer>
      <el-button @click="visible = false">取 消</el-button>
      <el-button
        v-if="mode != 'show'"
        type="primary"
        :loading="isSaveing"
        @click="submit()"
        >保 存</el-button
      >
    </template>
  </el-dialog>
</template>

<script>
export default {
  emits: ['success', 'closed'],
  data () {
    return {
      mode: "add",
      titleMap: {
        add: '新增',
        edit: '编辑',
        show: '查看'
      },
      visible: false,
      isSaveing: false,
      //表单数据
      form: {
        adminId: '',
        title: '',
        text: '',
        startTime: '',
        endTime: '',
        status: 0//是否启用 0启用 1禁用
      },
      //验证规则
      rules: {
        title: [
          { required: true, message: '请输入标题', }
        ],
        text: [
          { required: true, message: '请输入内容', }
        ],
        startTime: [
          {
            type: 'date',
            required: true,
            message: '请选择开始时间',
            trigger: 'change',
          },
        ],
        endTime: [
          {
            type: 'date',
            required: true,
            message: '请选择结束时间',
            trigger: 'change',
          },
        ],
      }
    }
  },
  mounted () {

  },

  methods: {
    //显示
    open (mode = 'add') {
      this.mode = mode
      this.visible = true
      let userInfo = this.$TOOL.data.get('USER_INFO')
      this.form.adminId = userInfo.userId
      return this
    },
    //表单提交方法
    submit () {
      this.$refs.dialogForm.validate(async (valid) => {
        if (valid) {
          this.isSaveing = true
          let res = null
          if (this.mode == 'add') {
            res = await this.$API.notice.addNotice.post(this.form)
          }
          this.isSaveing = false
          if (res.code == 200) {
            this.$emit('success', this.form, this.mode)
            this.visible = false
            this.$message.success("操作成功")
          } else {
            this.$alert(res.message, "提示", { type: 'error' })
          }
        }
      })
    },

    //表单注入数据
    setData (data) {
      this.form = data
    },
    // 设置就日期选择范围  今天之后
    disabledDate (time) {
      return time.getTime() < Date.now() - 8.64e7//如果没有后面的-8.64e7就是不可以选择
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
